home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / SOCKUTIL.C < prev    next >
Text File  |  1993-08-09  |  2KB  |  116 lines

  1. #include "global.h"
  2. #include "config.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "socket.h"
  6. #include "usock.h"
  7. #include "ax25.h"
  8. #include "tcp.h"
  9. #include "nr4.h"
  10.  
  11. /* Convert a socket (address + port) to an ascii string of the form
  12.  * aaa.aaa.aaa.aaa:ppppp
  13.  */
  14. char *
  15. psocket(void *p)        /* Pointer to structure to decode */
  16. {
  17. #if    defined(AX25) || defined(NETROM)
  18.     char tmp[AXBUF];
  19. #endif
  20.     static char buf[30];
  21.     union sp sp;
  22.  
  23.     sp.p = p;
  24.  
  25.     switch(sp.sa->sa_family) {
  26. /*
  27.     case AF_LOCAL:
  28.         break;
  29. */
  30.     case AF_INET: {
  31.         struct socket sock;
  32.         sock.address = sp.in->sin_addr.s_addr;
  33.         sock.port = sp.in->sin_port;
  34.         return pinet(&sock);
  35.       }
  36. #ifdef    AX25
  37.     case AF_AX25:
  38.         sprintf(buf,"%s on %s",
  39.             pax25(tmp,sp.ax->ax25_addr),
  40.             *sp.ax->iface ? sp.ax->iface : "???");
  41.         break;
  42. #endif
  43. #ifdef    NETROM
  44.     case AF_NETROM:
  45.         sprintf(buf,"%s @ ",pax25(tmp,sp.nr->nr_addr.user));
  46.         strcat(buf,pax25(tmp,sp.nr->nr_addr.node));
  47.         break;
  48. #endif
  49.     }
  50.     return buf;
  51. }
  52.  
  53. /* Return ASCII string giving reason for connection closing */
  54. char *
  55. sockerr(int s)        /* Socket index */
  56. {
  57.     struct usock *up;
  58.  
  59.     if((up = itop(s)) == NULLUSOCK){
  60.         return Badsocket;
  61.     }
  62.     switch(up->type){
  63.     case TYPE_TCP:
  64.         if(up->cb.tcb != NULLTCB)
  65.             return NULLCHAR;                        /* nothing wrong */
  66.         return Tcpreasons[up->errcodes[0]];
  67. #ifdef    AX25
  68.     case TYPE_AX25I:
  69.         if(up->cb.ax25 != NULLAX25)
  70.             return NULLCHAR;                        /* nothing wrong */
  71.         return Ax25reasons[up->errcodes[0]];
  72. #endif
  73. #ifdef    NETROM
  74.     case TYPE_NETROML4:
  75.         if(up->cb.nr4 != NULLNR4CB)
  76.             return NULLCHAR;                        /* nothing wrong */
  77.         return Nr4reasons[up->errcodes[0]];
  78. #endif
  79.     default:
  80.         errno = EOPNOTSUPP;                            /* not yet, anyway */
  81.         return NULLCHAR;
  82.     }
  83. }
  84.  
  85. /* Get state of protocol. Valid only for connection-oriented sockets. */
  86. char *
  87. sockstate(int s)        /* Socket index */
  88. {
  89.     struct usock *up;
  90.  
  91.     if((up = itop(s)) == NULLUSOCK){
  92.         return NULLCHAR;
  93.     }
  94.     if(up->cb.p == NULLCHAR){
  95.         errno = ENOTCONN;
  96.         return NULLCHAR;
  97.     }
  98.     switch(up->type){
  99.     case TYPE_TCP:
  100.         return Tcpstates[up->cb.tcb->state];
  101. #ifdef    AX25
  102.     case TYPE_AX25I:
  103.         return Ax25states[up->cb.ax25->state];
  104. #endif
  105. #ifdef    NETROM
  106.     case TYPE_NETROML4:
  107.         return Nr4states[up->cb.nr4->state];
  108. #endif
  109.     default:
  110.         /* Datagram sockets don't have state */
  111.         errno = EOPNOTSUPP;
  112.         return NULLCHAR;
  113.     }
  114. }
  115.  
  116.